﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace DwarfInterpreter
{
    class StringUtils
    {
        enum ReadingState 
        { 
            Space, 
            Char 
        };

        public static string RemoveExtraSpaces(string str)
        {
            string result = "";

            ReadingState state = ReadingState.Space; 

	        foreach (char ch in str)
	        {
                if ((ch == ' ') || (ch == '\t') || (ch == '\v'))
		        {
                    if (state == ReadingState.Char)
			        {
                        result += ch;
			        }
                    state = ReadingState.Space;
		        }
		        else
		        {
                    state = ReadingState.Char;
                    result += ch;
		        }		
	        }
	
            return result.Trim();
        }

        public static string AddBraketSpaces(string str)
        {
            str = str.Replace("(", " ( ");
            str = str.Replace(")", " ) ");
            str = str.Replace("[", " [ ");
            str = str.Replace("]", " ] ");

            return str;
        }

        public static bool IsSimpleString(string str)
        {
            string SIMPLE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";

            return IsSubsetOf(str, SIMPLE_CHARACTERS);
        }

        public static bool IsNumber(string str)
        {
            const string NUM_CHARS = "0123456789";

            return IsSubsetOf(str, NUM_CHARS);
        }

        private static bool IsSubsetOf(string needle, string haystack)
        {
            foreach (char ch in needle)
            {
                if (!haystack.Contains(ch))
                {
                    return false;
                }
            }

            return true;
        }

        public static bool IsFilePath(string str)
        {
            Regex rx = new Regex(@"^((?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.]+)\\(?:[\w]+\\)*\w([\w.])+|(?:[\w]+\\)*\w([\w.])+)$");
            return rx.IsMatch(str);
        }
    }
}
